import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    introduce: "",
    user_id: 0,
    pics: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    // receive data from tab
    const eventChannel = this.getOpenerEventChannel();
    var that = this;
    const info = eventChannel.on('info', function(data) {
      that.setData({
        info: data.info
      })
    })
    // get introduce and seller user_id which is not included in tab info
    this.getDetail();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo();
    this.getDetail();
  },

  // request tab_info
  async getInfo() {
    const data = {
      id: this.data.info.id
    }
    // const res = await request({
    //   url: "/tabinfo",
    //   data: data
    // })
    const info = {
      id: 2333,
      goods_name: "Test Case 2",
      goods_price: 2000,
      tags: [
        {
            id: 0,
            tag_name: "tag0",
            image_src: "/static/1.jpg"
        },
        {
            id: 2,
            tag_name: "tag2",
            image_src: "/static/2.jpeg"
        },
        {
            id: 85,
            tag_name: "tag85",
            image_src: "/static/3.jpeg"
        }
      ],
      image_src: "/static/g2.jpg",
      marked:true
    }
    this.setData({
      info: info
    })
  },

  // request detail
  async getDetail() {
    const data = {
      id: this.data.info.id
    }
    // const res = await request({
    //   url: "/detail",
    //   data: data
    // })
    const introduce = "香喷喷的蛋挞，只卖23.33元"
    const user_id = 12345678
    const pics = [
      "/static/g1.jpg",
      "/static/g3.jpg",
      "/static/g5.jpg"
    ]
    this.setData({
      introduce: introduce,
      user_id: user_id,
      pics: pics
    })
  },

  // 点击轮播图 放大预览
  handlePrevewImage(e) {
    // 1 先构造要预览的图片数组 
    const urls = this.data.pics;
    // 2 接收传递过来的图片url
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      current,
      urls
    });
  },

  handleMark() {
    const user_info=wx.getStorageSync("user_info");
    if(user_info){
      const marked = this.properties.info.marked;
      const data = {
        user_id: user_info.id,
        goods_id: this.properties.info.id,
        mark: !marked
      };
      // const res = await request({
      //   url:"/mark",
      //   data: data
      // })
      const status = true;
      const task = marked ? '取消收藏' : '收藏';
      if(status){
        wx.showToast({
          title: task + '成功！'
        })
        let info = this.properties.info;
        info.marked = !marked;
        this.setData({
          info: info
        })
      } else {
        wx.showToast({
          title: task + '失败，请稍后再试！'
        })
      }
    } else {
      wx.showModal({
        title: '您未登录',
        content: '是否跳转到登录界面？',
        success(res){
          if(res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
    }
  },

  // 点击 商品收藏图标
  handleContact() {
    const user_info=wx.getStorageSync("user_info");
    if(user_info){
      wx.navigateTo({
        url: '/pages/contact/contact?user_id=' + this.data.user_id
      })
    } else {
      wx.showModal({
        title: '您未登录',
        content: '是否跳转到登录界面？',
        success(res){
          if(res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
    }
  }
})